// pages/shezhi/shezhi.js

var api = require('../../js/api.js')
var utilBT = require('../../js/utilBT.js');
var utilH = require('../../js/utilHeart.js');
var utilAudio = require('../../js/utilAudio.js');


const app = getApp()

Page({

    /**
     * 页面的初始数据
     */
    data: {
        hrb: {
            rest_heart: 0,
            max_heart: 0,
            d5: 0,
            d6: 0,
            d7: 0,
            d8: 0,
            d9: 0,
        },
        audio_open: false,

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

        utilH.getStorage('hrb')
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        var that = this
        that.setData({
            hrb: app.globalData.hrb,
            audio_open: app.globalData.audio.open,
        })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    toShuoming: function (e) {
        let value = e.currentTarget.dataset.val
        wx.navigateTo({
            url: "/pages/index/shuoming/shuoming?val=" + value
        })
    },
    bindPickerChangeMax: function (e) {
        var that = this
        console.log('picker发送选择改变，携带值为', e.detail.value)
        console.log(that.data.xlmax[e.detail.value])
        that.setData({
            xlmaxIndex: e.detail.value
        })
    },
    bindPickerChangeJx: function (e) {
        var that = this
        console.log('picker发送选择改变，携带值为', e.detail.value)
        console.log(that.data.xljx[e.detail.value])
        that.setData({
            xljxiIndex: e.detail.value
        })
    },
    toFiveSection: function () {
        console.log('====>>toFiveSection')
        wx.navigateTo({
            url: "/pages/shezhi/fiveSection/fiveSection"
        })
    },
    toAerobicSection: function () {
        console.log('====>>toAerobicSection')
        wx.navigateTo({
            url: "/pages/shezhi/aerobicSection/aerobicSection"
        })
    },
    countdvm: function (e) {
        var that = this
        let v = e.detail.value
        if (v == null || v == '' || v == NaN) {
            v = 195
        }
        let max_h = parseInt(v)
        if (max_h < 110) {
            max_h = 110
        }
        if (max_h > 250) {
            max_h = 250
        }
        if (max_h <= app.globalData.hrb.rest_heart) {
            max_h = parseInt(app.globalData.hrb.rest_heart) + 1
        }
        app.globalData.hrb.max_heart = max_h

        utilH.setStorage('hrb', app.globalData.hrb, function (res) {
            utilH.countHeart()
            that.onShow()
        })


    },
    countdvr: function (e) {
        var that = this
        let v = e.detail.value
        if (v == null || v == '' || v == NaN) {
            v = 75
        }
        let max_r = parseInt(v)
        if (max_r < 20) {
            max_r = 20
        }
        if (max_r > 110) {
            max_r = 110
        }
        if (max_r > app.globalData.hrb.max_heart) {
            max_r = parseInt(app.globalData.hrb.max_heart) - 1
        }
        app.globalData.hrb.rest_heart = max_r

        utilH.setStorage('hrb', app.globalData.hrb, function (res) {
            utilH.countHeart()
            that.onShow()
        })

    },

    audioOpen: function (e) {
        utilAudio.setAudioOpen(e.detail.value)
    }
})
